#include "../include/data.h"


Data::Data()
{

}

Data::Data(const char *input_file, const char *output_file)
{
    int count = strlen(input_file);

    input = new char[count + 1];
    strcpy(input, input_file);

    count = strlen(output_file);
    output = new char[count + 1];
    strcpy(output, output_file);

}

Data::~Data()
{
      delete[] input;
      delete[] output;
}


void Data::init_data(unsigned int count)
{
    FILE *finput = fopen(input, "w+");

    for (unsigned int i = 0; i < count; i++)
        fprintf(finput, "%d ", rand() % 10000);

    fclose(finput);
}

int Data::read_data(FILE* f, int a[], int n)
{
    int i = 0;
    while(i < n && (fscanf(f, "%d", &a[i]) != EOF))
        i++;
    printf("read:%d integer/n", i);
    return i;
}


int Data::read_data(int *arr, unsigned int count)
{
    FILE *finput = fopen(input, "r+");

    if (finput == NULL)
        return -1;
     unsigned int  i ;
    for(i = 0; i < count ; i++)
    {
       if ( 0 >= fscanf(finput, "%d", &arr[i]))
          break;
    }
    fclose(finput);
    return i;
}


void Data::write_data(FILE* f, int a[], int n)
{
    for(int i = 0; i < n; ++i)
        fprintf(f, "%d ", a[i]);
}


int Data::write_data(int *arr, unsigned int count)
{
    FILE *foutput = fopen(output, "w+");

    if (NULL == foutput )
        return -1;

   unsigned int i ;
    for (i = 0; i < count; i++)
      if ( 0 > fprintf(foutput, " %d ", arr[i]))
          break;

    fclose(foutput);

    return i;
}


char* temp_filename(int index)
{
    char *tempfile = new char[100];
    sprintf(tempfile, "temp%d.txt", index);
    return tempfile;
}

